import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Toast {
  static OverlayEntry _overlayEntry; // toast靠它加到屏幕上
  static bool _always; //是否一直显示
  static DateTime _startedTime; // 开启一个新toast的当前时间，用于对比是否已经展示了足够时间
  static String _msg; // 提示内容
  static int _showTime; // toast显示时间
  static Timer t;

  static toast(BuildContext context,
      {String msg, int showTime = 5000, bool always = false}) async {
    assert(msg != null);
    _msg = msg;
    _startedTime = DateTime.now();
    _showTime = showTime;
    _always = always;
    //获取overlayState用来显示信息
    OverlayState overlayState = Overlay.of(context);

    if (_overlayEntry == null) {
      //主要靠_overlayEntry显示提示文字
      _overlayEntry = OverlayEntry(
          builder: (BuildContext context) =>
              Container(child: _buildToastWidget()));

      overlayState.insert(_overlayEntry);
      _overlayEntry.markNeedsBuild();
    } else {
      //重新绘制UI，类似setState
      _overlayEntry.markNeedsBuild();
    }

    if (t != null) {
      t.cancel();
    }

    t = Timer(Duration(milliseconds: _showTime), () {
      if (!_always) {
        if (_overlayEntry != null) {
          _overlayEntry.remove();
          _overlayEntry = null;
        }
      }
    });
  }

  //toast绘制
  static _buildToastWidget() {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: Stack(children: [
        Positioned(
          top: 0,
          left: 0,
          right: 0,
          bottom: 0,
          child: Container(
            decoration: BoxDecoration(
              color: Colors.black54,
            ),
            child: Center(
              //保证控件居中效果
              child: Container(
                margin: EdgeInsets.fromLTRB(25, 10, 25, 10),
                padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                decoration: ShapeDecoration(
                  color: Color(0xffffffff),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(3.0),
                    ),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.fromLTRB(15, 5, 15, 5),
                      child: SizedBox(
                          width: 24,
                          height: 24,
                          child: CircularProgressIndicator(
                            strokeWidth: 2,
                          )),
                    ),
                    Expanded(
                      child: Container(
                        padding: EdgeInsets.fromLTRB(15, 5, 15, 5),
                        child: Text(
                          _msg,
                          style: TextStyle(
                            fontSize: 14.0,
                            color: Colors.black,
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
        ),
      ]),
    );
  }
}
